﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Data.SqlClient;
public partial class Agency_company_biaoshu : System.Web.UI.Page
{
    string str_name = "";
   // string str_id = "";
    string str_sort = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];


        if (_Account != null)
        {
            if (int.Parse(_Account.Type) == 17 || int.Parse(_Account.Type) ==1)
            {
                str_name = Request.QueryString["accountname"].ToString().Trim();
               // str_id = Request.QueryString["accountid"].ToString().Trim();
                str_sort = Request.QueryString["sort"].ToString().Trim();              
                showGrid();
            }
            else
            {
                Response.Write("<script>window.alert('对不起，您无权查看！');</script>");
                Response.Write("<script>window.location.href='../HomePage/Default.aspx';</script>");
            }

        }
        else
        {
            Response.Write("<script>window.alert('对不起，请重新登录！');</script>");
            Response.Write("<script>window.location.href='../HomePage/Default.aspx';</script>");
        }

    }
    protected void showGrid()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "company as c inner join account as a  on c.accountid=a.accountid ";                                               //要查询的表明
        //要查询的表明
        dbPage.Key = "a.accountid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "accountname,companyname,a.accountid";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;                                                    //没有显示记录条数   
        dbPage.OrderBy = "a.accountid";        //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }
    public string fctConditions()
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string strwhere = "  checkstatus=1 and isbid_landing=1 ";
        string str_accountname = TextBox1.Text.ToString().Trim();
        string str_comname = TextBox2.Text.Trim();
        if (str_accountname != "")
        {
            string pn1 = "";//"(", pn2 = "(", pn3 = "(";
            int i = str_accountname.Split(' ').Length;
            for (int j = 0; j < i; j++)
            {
                if (str_accountname.Split(' ')[j].Trim() != "")
                {
                    pn1 += "accountname like '%" + str_accountname.Split(' ')[j].Trim() + "%' or ";
                    //pn2 += " or  companyname like '%" + str_comname.Split(' ')[j].Trim() + "%' ";
                    //pn3 += " or  companyname like '%" + str_comname.Split(' ')[j].Trim() + "%' ";
                }
            }
            if (pn1.Trim() != "")
            {
                pn1 = pn1.Substring(0, pn1.Length - 3);
                //pn1 = pn1.Remove(2, 3); pn2 = pn2.Remove(2, 3); pn3 = pn3.Remove(2, 3);              
                //pn1 = pn1 + " )"; pn2 = pn2 + " )"; pn3 = pn3 + " )";
                // strwhere += " and (" + pn1 + " or " + pn2 + " or " + pn3 + ")";
                strwhere += " and (" + pn1 + ")";
            }
            //strwhere += " and  accountname like '%" + str_accountname + "%'";
        }
        //if (str_comname != "")
        //{
        //    strwhere += " and companyname like '%" + str_comname + "%'";
        //}
        if (str_sort == "0")
        {
            strwhere += " and accountname like '" + str_name + "'";
        }

        if (str_comname != "")
        {

            string pn1 = "";//"(", pn2 = "(", pn3 = "(";
            int i = str_comname.Split(' ').Length;
            for (int j = 0; j < i; j++)
            {
                if (str_comname.Split(' ')[j].Trim() != "")
                {
                    pn1 += "companyname like '%" + str_comname.Split(' ')[j].Trim() + "%' or ";
                    //pn2 += " or  companyname like '%" + str_comname.Split(' ')[j].Trim() + "%' ";
                    //pn3 += " or  companyname like '%" + str_comname.Split(' ')[j].Trim() + "%' ";
                }
            }
            if (pn1.Trim() != "")
            {
                pn1 = pn1.Substring(0,pn1.Length - 3);
                //pn1 = pn1.Remove(2, 3); pn2 = pn2.Remove(2, 3); pn3 = pn3.Remove(2, 3);              
                //pn1 = pn1 + " )"; pn2 = pn2 + " )"; pn3 = pn3 + " )";
               // strwhere += " and (" + pn1 + " or " + pn2 + " or " + pn3 + ")";
                strwhere += " and ("+ pn1+")";
            }
           
        }
        return strwhere;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string strSql = "";
        string accountid = "";
        accountid = SysFun.ToTrim(Request.Form["chk"]);
        Db db = new Db();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string add_tjr = _Account.AccountName.ToString();
        string sql = " select companyname from company inner join account on company.accountid=account.accountid where accountname='" + str_name + "' ";
        DataTable gg = db.DataTable(sql);
        string str_usernameTB = gg.Rows[0][0].ToString();
        
            if (accountid != "")
        {
            strSql = @" insert into company_biaoshu(usercode_sc,usercode_tb,username_sc,username_tb,add_tjr) select accountname,'" + str_name + "',c.companyname,'" + str_usernameTB + "','" + add_tjr + "' from company as c inner join account as a  on c.accountid=a.accountid" +
                                                "  where a.accountid in (" + accountid + ") ";
            db.Command(strSql);
            
            Response.Write("<script>window.location.href='../Agency/company_biaoshu_dl.aspx';</script>");
        
        }
        else
        {
            Response.Write("<script>alert('请先选择企业！');</script>");        
        }

        db.Close();
        gg.Dispose();
        
    }



    protected void ToEcxul_Click(object sender, EventArgs e)
    {
        Db db = new Db();
        System.Data.IDataReader dr;

        string _strSql = " select usercode_sc as [企业注册号],username_sc as [企业名称],usercode_tb as 代领企业注册号,username_tb as 代领企业名称,add_tjr as 标书发放人,add_time as 发放时间 from company_biaoshu ";
        dr = db.DataReader(_strSql);
        GridView4.DataSource = dr;
        GridView4.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
       
        SysFun.WriteToExcel(GridView4, this.Page, "标书发放信息.xls");

    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！

    }
    //protected void Button2_Click(object sender, EventArgs e)
    //{
        //Db db = new Db();

        //string _strSql = " select a.usercode as 注册号,companyname as 企业名称,add_tjr as 标书发放人,add_time as 发放时间 from company_biaoshu   a inner  join company b on a.accountid=b.accountid";
        //DataTable gg = db.DataTable(_strSql);

        //GridView2.DataSource = gg;
        //GridView2.DataBind();
        //db.Close();

        //gg.Dispose();
        //SysFun.WriteToExcel(GridView2, this.Page, "目录信息.xls");

    //}
}
